package ws.jdbc.concurrent.driver.sqlserver

import java.sql.*

/**
 * Test the JDBC driver.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
class CDriverTest extends GroovyTestCase {
	Connection con
  String url = "jdbc:sqlserver://goldberg-wm:1433;username=sa;password=sg4765j;database=JDBCDriverTests"
  String sql = "Select * from Table1"

  void setUp() {
    con = DriverManager.getConnection(url)
    assertTrue con instanceof CConnection

    println "-------------------------------------------------------------------"
  }

  void printLine(ResultSet rs) {
    printf("%6d|%4d|  %s\n", rs.getRow(), rs.getInt(1), rs.getString(2))
  }

  void printSep() {
    println "\t--------"
  }

  void testForwardOnlyReadOnly() {
    println "testForwardOnlyReadOnly()"

    def stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
    assertTrue stmt instanceof CStatementForwardOnlyReadOnly

    ResultSet rs = stmt.executeQuery(sql)
    assertTrue rs instanceof CResultSetForwardOnlyReadOnly

    while(rs.next()) {
      printLine(rs)
    }
  }

  void testForwardOnlyUpdatable() {
    println "testForwardOnlyUpdatable()"

    def stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)
    assert stmt instanceof CStatementForwardOnlyUpdatable

    ResultSet rs = stmt.executeQuery(sql)
    assertTrue rs instanceof CResultSetForwardOnlyUpdatable
  }

  void testScrollableReadOnly() {
    println "testScrollableReadOnly()"

    def stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)
    assert stmt instanceof CStatementScrollableReadOnly

    ResultSet rs = stmt.executeQuery(sql)
    assertTrue rs instanceof CResultSetScrollableReadOnly

    rs.absolute(10)
    printLine(rs)

    printSep()

    4.times() {
      rs.relative(2)
      printLine(rs)
    }

    printSep()

    rs.afterLast()
    4.times() {
      rs.previous()
      printLine(rs)
    }
  }

  void testScrollableUpdatable() {
    println "testScrollableUpdatable()"

    def stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)
    assert stmt instanceof CStatementScrollableUpdatable

    ResultSet rs = stmt.executeQuery(sql)
    assertTrue rs instanceof CResultSetScrollableUpdatable

    rs.absolute(10)

    rs.updateInt(1, 0)
    rs.updateString(2, "ZZZ")
    rs.updateRow()

    printLine(rs)

    printSep()
    
    //rs.moveToInsertRow()
    rs.updateInt(1, 123)
    rs.updateString(2, "new")
    rs.insertRow()
    //rs.moveToCurrentRow()

    rs.last()
    printLine(rs)
  }
}

